﻿
using Macad.Core;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Interaction.Editors.Shapes;
using Macad.Occt;
using SpCad;
using SpCad.Tools;
using SpCad.Utils;
using static Macad.Interaction.ToggleSubshapesAction;
using EventArgs = Macad.Interaction.ToggleSubshapesAction.EventArgs;

namespace SPCAM.Plugin.Cam.Tools
{
    public class SelectEdgeTools : Tool
    {
        /// <summary>
        /// 选择的边框
        /// </summary>
        public List<Subshape> Edges = new List<Subshape>();
        readonly Body _TargetBody;
        Prs3d_Drawer _TopmostHighlightDrawer;
        Subshape _ChangedSubshape;
        public event ToggleSubshapesAction.EventHandler Finished;
        public SelectEdgeTools(Body targetBody, List<Subshape> sheld = null)
        {
            _TargetBody = targetBody;
            if (sheld != null)
            {
                Edges.AddRange(sheld);
            }
        }
        protected override bool OnStart()
        {
            var newAction = new SelectAction(SubshapeTypes.Edge, _TargetBody);
            newAction.WorkspaceController = SpServer.Instance.WorkspaceController;
            if (!StartAction(newAction))
            {
                return false;
            }
            newAction.Finished += NewAction_Finished;
            return true;
        }

        private void NewAction_Finished(SelectAction sender, SelectAction.EventArgs args)
        {

        }

        void _ProcessMouseSelect(MouseEventData data)
        {

            foreach (var detectedShape in data.DetectedShapes)
            {
                var subshape = Edges.FirstOrDefault(sh => sh.Shape.IsEqual(detectedShape));
                var edgeshap = detectedShape.ToEdge();

                var sp = ShapeTools.Create(edgeshap);
                if (subshape != null)
                {
                    subshape.IsSelected = !subshape.IsSelected;
                    subshape.AisShape.SetColor((subshape.IsSelected ? Colors.FilteredSubshapesHot : Colors.FilteredSubshapes).ToQuantityColor());
                    _ChangedSubshape = subshape;

                    EventArgs args = new()
                    {
                        ChangedSubshape = _ChangedSubshape,
                        MouseEventData = data
                    };
                    //  Finished?.Invoke(this, args);
                    data.ForceReDetection = true;
                    break;
                }
            }
        }
        public void ClearSubshapes()
        {
            foreach (var subshape in Edges)
            {
                if (subshape.AisShape != null)
                {
                    WorkspaceController.Workspace.AisContext.Remove(subshape.AisShape, false);
                }
            }
            Edges.Clear();
        }
        public void AddSubshape(TopoDS_Shape shape, Trsf trsf, bool isSelected)
        {
            Subshape subshape;
            var sdsub = Edges.FirstOrDefault(sh => sh.Shape.IsEqual(shape));
            if (sdsub == null)
            {
                subshape = new Subshape()
                {
                    Shape = shape,
                    IsSelected = isSelected,
                    AisShape = new AIS_Shape(shape)
                };
            }
            else
            {
                subshape = sdsub;
            }



            var parr = Prs3d_Arrow.DrawSegments(new Pnt(0, 0, 0), new Dir(0, 0, 1), 0, 10, 0);
            subshape.AisShape.Attributes().SetLineArrowDraw(true);
            //  subshape.AisShape.Attributes().SetArrowAspect(parr);
            //  tols.Create(subshape.AisShape);
            //Debug.WriteLine(string.Format("Added component to sum: {0}", Subshapes.Count));

            subshape.AisShape.SetLocalTransformation(trsf);
            subshape.AisShape.SetColor((isSelected ? Colors.FilteredSubshapesHot : Colors.FilteredSubshapes).ToQuantityColor());
            subshape.AisShape.Attributes().WireAspect().SetWidth(4);
            subshape.AisShape.SetMaterial(new Graphic3d_MaterialAspect(Graphic3d_NameOfMaterial.Plastified));
            subshape.AisShape.SetPolygonOffsets(1, 0.999f, -5.0f);
            subshape.AisShape.SetZLayer(-2 /* Top */);

            if (_TopmostHighlightDrawer == null)
            {
                _TopmostHighlightDrawer = new Prs3d_Drawer();
                _TopmostHighlightDrawer.SetLink(WorkspaceController.Workspace.AisContext.HighlightStyle(Prs3d_TypeOfHighlight.Dynamic));
                _TopmostHighlightDrawer.SetZLayer(-2 /* Top */);
                _TopmostHighlightDrawer.SetDisplayMode(1);
                _TopmostHighlightDrawer.SetColor(Colors.Highlight.ToQuantityColor());
            }
            subshape.AisShape.SetDynamicHilightAttributes(_TopmostHighlightDrawer);

            WorkspaceController.Workspace.AisContext.Display(subshape.AisShape, false);

            WorkspaceController.Workspace.AisContext.Activate(subshape.AisShape, 0, false);
            WorkspaceController.Workspace.AisContext.SetSelectionSensitivity(subshape.AisShape, 0, 10);

            Edges.Add(subshape);
        }
        public bool IsFinished { get; protected set; }
        public override bool OnMouseUp(MouseEventData data)
        {
            if (!IsFinished)
            {
                _ProcessMouseSelect(data);
            }
            return true;
        }

    }
}
